// src/controllers/adminController.js
const User = require('../models/User');
const Message = require('../models/Message');
const { logger } = require('../utils/logger');

/**
 * 获取所有用户
 */
exports.getAllUsers = async (req, res, next) => {
  try {
    const users = await User.find().select('-password');
    logger.info('Fetched all users by admin');
    res.status(200).json({ users });
  } catch (err) {
    logger.error('Get all users error:', err);
    next(err);
  }
};

/**
 * 删除用户
 */
exports.deleteUser = async (req, res, next) => {
  try {
    const { userId } = req.params;
    await User.findByIdAndDelete(userId);
    logger.info(`User deleted: ${userId}`);
    res.status(200).json({ message: 'User deleted' });
  } catch (err) {
    logger.error('Delete user error:', err);
    next(err);
  }
};

/**
 * 获取所有消息
 */
exports.getAllMessages = async (req, res, next) => {
  try {
    const { limit = 50 } = req.query;
    const messages = await Message.find()
      .sort({ timestamp: -1 })
      .limit(parseInt(limit));
    logger.info('Fetched all messages by admin');
    res.status(200).json({ messages });
  } catch (err) {
    logger.error('Get all messages error:', err);
    next(err);
  }
};